interface VenueData {
  name?: string;
  description?: string;
  address?: string;
  city?: string;
  country?: string;
  capacity?: string | number;
  contactPhone?: string;
  contactEmail?: string;
  website?: string;
  latitude?: string | number;
  longitude?: string | number;
  status?: string;
}

interface ValidationResult {
  isValid: boolean;
  errors: string[];
}

export function validateVenueData(data: VenueData, isUpdate: boolean = false): ValidationResult {
  const errors: string[] = [];

  // 必填字段验证
  if (!isUpdate || data.name !== undefined) {
    if (!data.name || data.name.trim().length === 0) {
      errors.push('场馆名称不能为空');
    } else if (data.name.length > 100) {
      errors.push('场馆名称不能超过100个字符');
    }
  }

  if (!isUpdate || data.address !== undefined) {
    if (!data.address || data.address.trim().length === 0) {
      errors.push('场馆地址不能为空');
    } else if (data.address.length > 200) {
      errors.push('场馆地址不能超过200个字符');
    }
  }

  if (!isUpdate || data.city !== undefined) {
    if (!data.city || data.city.trim().length === 0) {
      errors.push('城市不能为空');
    } else if (data.city.length > 50) {
      errors.push('城市名称不能超过50个字符');
    }
  }

  if (!isUpdate || data.country !== undefined) {
    if (!data.country || data.country.trim().length === 0) {
      errors.push('国家不能为空');
    } else if (data.country.length > 50) {
      errors.push('国家名称不能超过50个字符');
    }
  }

  // 可选字段验证
  if (data.description !== undefined && data.description && data.description.length > 65535) {
    errors.push('场馆描述不能超过65535个字符');
  }

  if (data.capacity !== undefined && data.capacity !== null && data.capacity !== '') {
    const capacity = parseInt(data.capacity.toString());
    if (isNaN(capacity) || capacity < 0) {
      errors.push('场馆容量必须是正整数');
    }
  }

  if (data.contactPhone !== undefined && data.contactPhone) {
    const phoneRegex = /^[\+]?[1-9][\d]{0,15}$/;
    if (!phoneRegex.test(data.contactPhone)) {
      errors.push('联系电话格式不正确');
    }
    if (data.contactPhone.length > 20) {
      errors.push('联系电话不能超过20个字符');
    }
  }

  if (data.contactEmail !== undefined && data.contactEmail) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(data.contactEmail)) {
      errors.push('联系邮箱格式不正确');
    }
    if (data.contactEmail.length > 100) {
      errors.push('联系邮箱不能超过100个字符');
    }
  }

  if (data.website !== undefined && data.website) {
    const urlRegex = /^https?:\/\/.+/;
    if (!urlRegex.test(data.website)) {
      errors.push('网站地址必须以http://或https://开头');
    }
    if (data.website.length > 200) {
      errors.push('网站地址不能超过200个字符');
    }
  }

  if (data.latitude !== undefined && data.latitude !== null && data.latitude !== '') {
    const lat = parseFloat(data.latitude.toString());
    if (isNaN(lat) || lat < -90 || lat > 90) {
      errors.push('纬度必须在-90到90之间');
    }
  }

  if (data.longitude !== undefined && data.longitude !== null && data.longitude !== '') {
    const lng = parseFloat(data.longitude.toString());
    if (isNaN(lng) || lng < -180 || lng > 180) {
      errors.push('经度必须在-180到180之间');
    }
  }

  if (data.status !== undefined && data.status) {
    const validStatuses = ['ACTIVE', 'INACTIVE', 'MAINTENANCE'];
    if (!validStatuses.includes(data.status)) {
      errors.push('状态值无效，必须是ACTIVE、INACTIVE或MAINTENANCE之一');
    }
  }

  return {
    isValid: errors.length === 0,
    errors
  };
}
